Soru & Cevap

Java Android bilenler lütfen

02.11.2016 - 10:51

Aşağıya MainActivity sayfamı koyuyorum. Sorum şu: 

protected void onCreate(Bundle savedInstanceState) içerisine aşağıdaki webviewle başlayan kodları koyduğum zaman uygulama derleniyor ama hata veriyor. Yani webview'le başlayan hiçbirisini kabul etmiyor. 

        webViewClient = new CustomWebViewClient();//CustomWebViewClient classdan webViewClient objesi oluşturuyoruz
        webview.getSettings().setBuiltInZoomControls(true); //zoom yapılmasına izin verir
        webview.getSettings().setSupportZoom(true);
        webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webview.getSettings().setAllowFileAccess(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebViewClient(webViewClient); //oluşturduğumuz webViewClient objesini webViewımıza set ediyoruz

YUKARDAKİ KODLARI    public void onBackPressed() İÇERİSİNE KOYDUĞUMDA ÇALIŞIYOR AMA HALİYLE GERİ GİDİNCE ÇALIŞIYOR :)

SORUN NERDE! KAFAYI SIYIRACAĞIM

 

 

MAİNACTİVİTY SAYFAM:

package example.com.materialnavigationdrawer.activities;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.onesignal.OneSignal;

import butterknife.Bind;
import butterknife.ButterKnife;
import example.com.materialnavigationdrawer.R;
import example.com.materialnavigationdrawer.fragments.ButterknifeFragment;
import example.com.materialnavigationdrawer.fragments.CricinfoFragment;
import example.com.materialnavigationdrawer.fragments.DraftFragment;
import example.com.materialnavigationdrawer.fragments.GoogleFragment;
import example.com.materialnavigationdrawer.fragments.InboxFragment;
import example.com.materialnavigationdrawer.fragments.MaterialDesignGoogleFragment;
import example.com.materialnavigationdrawer.fragments.SentMailFrag;
import example.com.materialnavigationdrawer.fragments.StarredFragment;

/**
 * Created by jarvis on 10-Mar-16
 * at  11:52 AM .
 */
public class MainActivity extends AppCompatActivity {

    //Defining Variables
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Bind(R.id.navigation_view)
    NavigationView navigationView;

    private DrawerLayout drawerLayout;



    WebView webview;//webview id ile oluşturuldu
    @Override
    public void onBackPressed() {

        webview = (WebView) findViewById(R.id.googleWebView);


    if (webview.canGoBack()) {
            webview.goBack();
        } else {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
            alertDialog.setTitle("KAMU BİLGİ");
            alertDialog.setMessage("Çıkmak Istediğinize Emin misiniz?");
            alertDialog.setPositiveButton("Evet", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            alertDialog.setNegativeButton("Hayır", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            alertDialog.show();
        }



    }

    private CustomWebViewClient webViewClient;
    private String Url = "http://www.xxx.com";
    ProgressDialog mProgressDialog;



    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mProgressDialog = new ProgressDialog(this);//ProgressDialog objesi oluşturuyoruz
        mProgressDialog.setMessage("Yükleniyor...");//ProgressDialog Yükleniyor yazısı

        webViewClient = new CustomWebViewClient();//CustomWebViewClient classdan webViewClient objesi oluşturuyoruz

        webview.getSettings().setBuiltInZoomControls(true); //zoom yapılmasına izin verir
        webview.getSettings().setSupportZoom(true);
        webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webview.getSettings().setAllowFileAccess(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebViewClient(webViewClient); //oluşturduğumuz webViewClient objesini webViewımıza set ediyoruz


        OneSignal.startInit(this).init();


        ButterKnife.bind(this);
        //  setting it as the actionbar
        setSupportActionBar(toolbar);

        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        tx.replace(R.id.frame, new GoogleFragment());
        tx.commit();
        //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {


                //Checking if the item is in checked state or not, if not make it in checked state
                if (menuItem.isChecked()) menuItem.setChecked(false);
                else menuItem.setChecked(true);

                //Closing drawer on item click
                drawerLayout.closeDrawers();

                //Check to see which item was being clicked and perform appropriate action
                switch (menuItem.getItemId()) {


                    //Replacing the main content with ContentFragment Which is our Inbox View;
                    case R.id.inbox:
                        InboxFragment contentFragment = new InboxFragment();
                        FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction1.replace(R.id.frame, contentFragment);
                        fragmentTransaction1.commit();
                        toolbar.setTitle("Gelen Kutusu");
                        return true;

                    case R.id.starred:
                        StarredFragment starredFragment = new StarredFragment();
                        FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction2.replace(R.id.frame, starredFragment);
                        fragmentTransaction2.commit();
                        toolbar.setTitle("Başlangıç");
                        return true;
                    case R.id.sent_mail:
                        SentMailFrag sentMailFragment = new SentMailFrag();
                        FragmentTransaction fragmentTransaction3 = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction3.replace(R.id.frame, sentMailFragment);
                        fragmentTransaction3.commit();
                        toolbar.setTitle("Mail Gönder");
                        return true;
                    case R.id.drafts:
                        DraftFragment draftFragment = new DraftFragment();
                        FragmentTransaction fragmentTransaction4 = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction4.replace(R.id.frame, draftFragment);
                        fragmentTransaction4.commit();
                        toolbar.setTitle("Çöp Kutusu");
                        return true;
                    case R.id.google:
                        GoogleFragment googleFragment = new GoogleFragment();
                        FragmentTransaction fragmentTransaction5 = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction5.replace(R.id.frame, googleFragment);
                        fragmentTransaction5.commit();
                        toolbar.setTitle("Ana Sayfa");
                        return true;
                    case R.id.cricinfo:
                        CricinfoFragment cricinfoFragment = new CricinfoFragment();
                        FragmentTransaction fragmentTransaction6 = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction6.replace(R.id.frame, cricinfoFragment);
                        fragmentTransaction6.commit();
                        toolbar.setTitle("Resim Galeri");
                        return true;
                    case R.id.butterknife:
                        ButterknifeFragment butterknifeFragment = new ButterknifeFragment();
                        FragmentTransaction fragmentTransaction7 = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction7.replace(R.id.frame, butterknifeFragment);
                        fragmentTransaction7.commit();
                        toolbar.setTitle("Video Galeri");
                        return true;
                    case R.id.footerItem:

                        MaterialDesignGoogleFragment materialDesignGoogleFragmentHere = new MaterialDesignGoogleFragment();
                        FragmentTransaction fragmentTransaction8 = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction8.replace(R.id.frame, materialDesignGoogleFragmentHere);
                        fragmentTransaction8.commit();
                        toolbar.setTitle("Soru - Cevap");
                        return true;
                    default:
                        Toast.makeText(MainActivity.this, "Default case", Toast.LENGTH_LONG).show();
                        return true;

                }
            }
        });

        // Initializing Drawer Layout and ActionBarToggle
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer) {

            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank

                super.onDrawerOpened(drawerView);
            }
        };

        //Setting the actionbarToggle to drawer layout
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        //calling sync state is necessay or else your hamburger icon wont show up
        actionBarDrawerToggle.syncState();


    }




    private class CustomWebViewClient extends WebViewClient {
        //Alttaki methodların hepsini kullanmak zorunda deilsiniz
        //Hangisi işinize yarıyorsa onu kullanabilirsiniz.
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) { //Sayfa yüklenirken çalışır
            super.onPageStarted(view, url, favicon);

            if(!mProgressDialog.isShowing())//mProgressDialog açık mı kontrol ediliyor
            {
                mProgressDialog.show();//mProgressDialog açık değilse açılıyor yani gösteriliyor ve yükleniyor yazısı çıkıyor
            }

        }

        @Override
        public void onPageFinished(WebView view, String url) {//sayfamız yüklendiğinde çalışıyor.
            super.onPageFinished(view, url);

            if(mProgressDialog.isShowing()){//mProgressDialog açık mı kontrol
                mProgressDialog.dismiss();//mProgressDialog açıksa kapatılıyor
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // Bu method açılan sayfa içinden başka linklere tıklandığında açılmasına yarıyor.
            //Bu methodu override etmez yada edip içini boş bırakırsanız ilk url den açılan sayfa dışında başka sayfaya geçiş yapamaz

            view.loadUrl(url);//yeni tıklanan url i açıyor
            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
            //BU method webview yüklenirken herhangi bir hatayla karşilaşilırsa hata kodu dönüyor.
            //Dönen hata koduna göre kullanıcıyı bilgilendirebilir yada gerekli işlemleri yapabilirsiniz
            //errorCode ile hatayı alabilirsiniz
            //	if(errorCode==-8){
            //		Timeout
            //	} şeklinde kullanabilirsiniz

            //Hata Kodları aşağıdadır...

	    	/*
	    	 *  /** Generic error
		    public static final int ERROR_UNKNOWN = -1;

		    /** Server or proxy hostname lookup failed
		    public static final int ERROR_HOST_LOOKUP = -2;

		    /** Unsupported authentication scheme (not basic or digest)
		    public static final int ERROR_UNSUPPORTED_AUTH_SCHEME = -3;

		    /** User authentication failed on server
		    public static final int ERROR_AUTHENTICATION = -4;

		    /** User authentication failed on proxy
		    public static final int ERROR_PROXY_AUTHENTICATION = -5;

		    /** Failed to connect to the server
		    public static final int ERROR_CONNECT = -6;

		    /** Failed to read or write to the server
		    public static final int ERROR_IO = -7;

		    /** Connection timed out
		    public static final int ERROR_TIMEOUT = -8;

		    /** Too many redirects
		    public static final int ERROR_REDIRECT_LOOP = -9;

		    /** Unsupported URI scheme
		    public static final int ERROR_UNSUPPORTED_SCHEME = -10;

		    /** Failed to perform SSL handshake
		    public static final int ERROR_FAILED_SSL_HANDSHAKE = -11;

		    /** Malformed URL
		    public static final int ERROR_BAD_URL = -12;

		    /** Generic file error
		    public static final int ERROR_FILE = -13;

		    /** File not found
		    public static final int ERROR_FILE_NOT_FOUND = -14;

		    /** Too many requests during this load
		    public static final int ERROR_TOO_MANY_REQUESTS = -15;
	    	*/

        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}



 

14 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-82231-1518190944.jpg
ysfyltrk
03.11.2016 - 05:57

Tarayıcıyı (webview) onBackPressed içerisinde tanımlamışsınız.webview ı onCreate içerisinde tanımlayarak deneyin.

webview = (WebView) findViewById(R.id.googleWebView);

kısmını onCreate içerisine yerleştirerek deneyin.